// pages/index/index.js
import { PubSub } from "pubsub-js";
import requests from "../../utils/requests";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        bannerList:[],
        recommendList:[],
        topListList:[],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
         // 请求网易云api轮播图
        this.requestindexWxml();
        this.requestindexRecommend();
        this.requesttopList();

        //监听playsong事件
    },
    toMusic(event){
        let musicid = event.currentTarget.dataset.musicid
        //console.log(event.currentTarget.dataset.musicid);
        wx.navigateTo({
          url: '/pages/playsong/playsong?musicId=' + musicid,
        })
    },
    

    todailyrecommend(){
        //console.log("hello");
        wx.navigateTo({
          url: '/pages/daily_recommend/daily_recommend',
        })
    },
    // 请求网易云api轮播图
    async requestindexWxml(){
        let result = await requests( '/banner',{type:2});
        if(result.code === 200){
            this.setData({
                bannerList : result.banners
            })
        }
    },
    //请求推荐歌单
    async requestindexRecommend(){
        let result = await requests( '/personalized',{limit:10});
        if(result.code === 200){
            this.setData({
                recommendList : result.result
            })
        }
    },
    //请求排行榜
    async requesttopList(){
        let index = 0;
        let resultarray = [];
        while(index<5){
            let result = await requests( '/top/list',{idx:index++});
            if(result.code === 200){
                let resultitem = {
                    name : result.playlist.name,
                    tracks : result.playlist.tracks.slice(0,3)
                }
                resultarray.push(resultitem);
                //console.log(result);
                this.setData({
                    topListList:resultarray
                })
            }
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})